package com.hq.fxh.No0_ChengQu.Activity;

/**
 * Created by admin on 2017/8/3.
 */

import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.hq.fxh.No2_BBS.Fragment.Fragment_BBS;
import com.hq.fxh.No1_Infromation.Infor.Fragment.InformationFragment;
import com.hq.fxh.No0_ChengQu.Adapter.PAdapter_Home;
import com.hq.fxh.No0_ChengQu.Fragment.HomeFragment;
import com.hq.fxh.No3_TuiGuang.Fragment.InfoFragment;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.CustomerView.NoScrollViewPager;

import java.util.ArrayList;


import cn.jiguang.analytics.android.api.JAnalyticsInterface;


/**
 * Created by admin on 2017/8/3.
 */

public class HomeActivity extends BaseActivity {

    public NoScrollViewPager mViewPager;
    InfoFragment mFragment3;
    HomeFragment mFragment4;
    HomeFragment mFragment5;
    Fragment_BBS mFragment2;
    InformationFragment mFragment1;
    ArrayList<Fragment> fragments;
    public LinearLayout home;
    PAdapter_Home mPagerAdapter;

    private TabLayout mTabLayout;

    private ArrayList<String> titles = new ArrayList<>();

    private boolean isSelected = false;

    private int[] tabIcons = {
            R.drawable.inforiconchange,
            R.drawable.bbsiconchange,
            R.drawable.tuiguangiconchange,
            R.drawable.messageiconchange,
            R.drawable.meiconchange,
    };

    @Override
    protected int getLayoutId() {
        return R.layout.custom_tabitem_layout;
    }

    private void initListenter() {

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                changeTabStatus(tab, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                changeTabStatus(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 2) {
                    setSysColor(R.color.red,true);
                } else {
                    setSysColor(R.color.red,false);

                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initView() {
        mViewPager = (NoScrollViewPager) findViewById(R.id.view_pager);
        mTabLayout = (TabLayout) findViewById(R.id.toolbar_tab);
        home = (LinearLayout) findViewById(R.id.home);

    }

    @Override
    protected void initData() {
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                LogUtils.e("homePage页数" + position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        getData();
        initListenter();
    }


    private void changeTabStatus(TabLayout.Tab tab, boolean selected) {
        View view = tab.getCustomView();
        ImageView imgTitle = (ImageView) view.findViewById(R.id.img_title);
        TextView txtTitle = (TextView) view.findViewById(R.id.txt_title);
        imgTitle.setVisibility(View.VISIBLE);
        if (selected) {
            txtTitle.setTextColor(Color.parseColor("#0EA73C"));
        } else {
            txtTitle.setTextColor(Color.parseColor("#7f7f7f"));
        }
    }

    public View getTabView(final int position) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_tab, null);
        TextView txtTitle = (TextView) view.findViewById(R.id.txt_title);
        final ImageView imgTitle = (ImageView) view.findViewById(R.id.img_title);
        txtTitle.setText(titles.get(position));
        imgTitle.setImageResource(tabIcons[position]);
        if (position == 0) {
            txtTitle.setTextColor(Color.parseColor("#057523"));
        } else {
            txtTitle.setTextColor(Color.parseColor("#ced0d3"));
        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mViewPager.setCurrentItem(position);
                imgTitle.setSelected(true);

            }


        });
        return view;
    }

    @Override
    protected void onStart() {
        super.onStart();
        JAnalyticsInterface.onPageStart(this, "首页面");
    }

    @Override
    protected void onStop() {
        super.onStop();
        JAnalyticsInterface.onPageEnd(this, "首页面");

    }

    public void getData() {
        mFragment5 = new HomeFragment();
        mFragment5.initData('a', 'z');
        mFragment4 = new HomeFragment();
        mFragment4.initData('a', 'z');
        mFragment3 = new InfoFragment();
        mFragment2 = new Fragment_BBS();
        mFragment1 = new InformationFragment();
        if (fragments == null) {
            fragments = new ArrayList<>();
        }
        fragments.add(mFragment1);
        fragments.add(mFragment2);
        fragments.add(mFragment3);
        fragments.add(mFragment4);
        fragments.add(mFragment5);

        titles.add("资讯");
        titles.add("论坛");
        titles.add("推广");
        titles.add("消息");
        titles.add("我的");
        mViewPager.setOffscreenPageLimit(5);

        mPagerAdapter = new PAdapter_Home(getSupportFragmentManager(), fragments);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.getTabAt(0).setCustomView(getTabView(0));
        mTabLayout.getTabAt(1).setCustomView(getTabView(1));
        mTabLayout.getTabAt(2).setCustomView(getTabView(2));
        mTabLayout.getTabAt(3).setCustomView(getTabView(3));
        mTabLayout.getTabAt(4).setCustomView(getTabView(4));

    }
}
